package freespiritsocial.io.user.data.bindAdapter

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.text.*
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import android.widget.CalendarView
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.view.isVisible
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import freespiritsocial.io.user.R
import freespiritsocial.io.user.ext.circleImageUrlWithBorder
import freespiritsocial.io.user.ext.loadImageWithCorner
import me.hgj.jetpackmvvm.ext.view.textString

/**
 * 描述　: 自定义 BindingAdapter
 */

object CustomBindAdapter {

    @BindingAdapter(value = ["onRefresh","onLoadMore"],requireAll = false)
    @JvmStatic
    fun refreshLoadMore(
        view: SmartRefreshLayout,
        onRefresh: OnRefreshListener? = null,
        onLoadMore: OnLoadMoreListener? = null
    ) {
        view.setOnRefreshListener(onRefresh)
        view.setOnLoadMoreListener(onLoadMore)

    }


    @BindingAdapter(value = ["onDateSelect"])
    @JvmStatic
    fun onDateSelect(view: CalendarView,listener: CalendarView.OnDateChangeListener) {
        view.setOnDateChangeListener(listener)
    }

    @BindingAdapter(value = ["goBack"])
    @JvmStatic
    fun goBack(view: Toolbar,listener: Callback) {
        view.setNavigationOnClickListener {
            listener.onClick()
        }
    }

    @BindingAdapter(value = ["setSelect"])
    @JvmStatic
    fun setSelect(view: View,select: Boolean) {
        view.isSelected = select
    }

    @BindingAdapter(value = ["drawEnd"])
    @JvmStatic
    fun drawEnd(view: TextView,resId: Drawable?) {
        view.setCompoundDrawablesWithIntrinsicBounds(null,null,resId,null)
    }

    @BindingAdapter(value = ["setVisible"])
    @JvmStatic
    fun setVisible(view: View,select: Boolean) {
        view.isVisible = select
    }

    @SuppressLint("ResourceAsColor")
    @BindingAdapter(
        value = ["firstText","secondText","firstColor","secondColor"],
        requireAll = true
    )
    @JvmStatic
    fun setText(
        view: TextView,
        firstText: String,
        secondText: String,
        firstColor: Int,
        secondColor: Int
    ) {
        view.text = SpannableStringBuilder().apply {
            append(firstText)
            setSpan(
                ForegroundColorSpan(firstColor),
                0,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )

            val offset = length
            if (secondText.isEmpty()) return@apply
            append(secondText)
            setSpan(
                ForegroundColorSpan(secondColor),
                offset,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }

    @BindingAdapter(value = ["showPwd"])
    @JvmStatic
    fun showPwd(view: EditText,boolean: Boolean) {
        if (boolean) {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        view.setSelection(view.textString().length)
    }

    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView,url: String?) {
        Glide.with(view.context.applicationContext)
            .load(url)
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }


    @BindingAdapter(value = ["cornerImageUrl","corner"],requireAll = true)
    @JvmStatic
    fun cornerImageUrl(view: ImageView,cornerImageUrl: String?,corner: Int) {
        //TODO 测试
        cornerImageUrl?.let {
            view.loadImageWithCorner(it,corner)
        }
    }

    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView,url: String?) {
        Glide.with(view.context.applicationContext)
            .load(url)
            .apply(RequestOptions().centerCrop().transform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }


    @BindingAdapter(value = ["circleImageUrlWithBorder","border"],requireAll = false)
    @JvmStatic
    fun circleImageUrlWithBorder(view: ImageView,url: String?,border: Int = 0) {
        view.circleImageUrlWithBorder(url,border)
    }

    @BindingAdapter(value = ["boldText","normalText"],requireAll = true)
    @JvmStatic
    fun setBoldSpannable(view: TextView,boldText: String?,normalText: String?) {
        view.text =
            SpannableStringBuilder().append(boldText ?: "0",StyleSpan(Typeface.BOLD),Spannable.SPAN_INCLUSIVE_EXCLUSIVE).append(" ")
                .append(normalText)
    }


    @BindingAdapter(value = ["afterTextChanged"])
    @JvmStatic
    fun EditText.afterTextChanged(action: (String) -> Unit) {
        addTextChangedListener(object: TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?,start: Int,count: Int,after: Int) {

            }

            override fun onTextChanged(s: CharSequence?,start: Int,before: Int,count: Int) {
                action(s.toString())
            }
        })
    }

    interface Callback {
        fun onClick()
    }
}